<template>
	<message-toast 
		:show="showToast" 
		:message="currentMessage" 
		:duration="3000"
		@close="handleClose"
		@click="handleClick"
	/>
</template>

<script setup>
	import { ref, onMounted, onUnmounted } from 'vue'
	import messageManager from '@/utils/message-manager.js'
	import MessageToast from '@/components/message-toast/message-toast.vue'
	
	// 响应式数据
	const showToast = ref(false)
	const currentMessage = ref({})
	
	// 消息监听器
	const messageListener = (type, data) => {
		if (type === 'show') {
			currentMessage.value = data
			showToast.value = true
		} else if (type === 'hide') {
			showToast.value = false
		}
	}
	
	// 处理关闭事件
	const handleClose = () => {
		showToast.value = false
		messageManager.hideMessage()
	}
	
	// 处理点击事件
	const handleClick = (message) => {
		console.log('消息被点击:', message)
		
		// 根据消息类型进行不同的处理
		switch (message.type) {
			case 'homework':
				handleHomeworkClick(message)
				break
			case 'system':
				handleSystemClick(message)
				break
			case 'ai':
				handleAIClick(message)
				break
			default:
				handleDefaultClick(message)
		}
	}
	
	// 处理作业消息点击
	const handleHomeworkClick = (message) => {
		if (message.homeworkId) {
			// 跳转到作业详情页面
			uni.navigateTo({
				url: `/pages/homework/detail?id=${message.homeworkId}`
			})
		} else {
			// 跳转到作业列表页面
			uni.navigateTo({
				url: '/pages/homework/homework'
			})
		}
	}
	
	// 处理系统消息点击
	const handleSystemClick = (message) => {
		// 显示系统消息详情
		uni.showModal({
			title: '系统消息',
			content: message.content,
			showCancel: false,
			confirmText: '知道了'
		})
	}
	
	// 处理AI助手消息点击
	const handleAIClick = (message) => {
		// 跳转到AI助手页面或显示AI对话
		uni.showToast({
			title: 'AI助手功能开发中',
			icon: 'none'
		})
	}
	
	// 处理默认消息点击
	const handleDefaultClick = (message) => {
		// 默认行为：显示消息详情
		console.log('默认消息点击处理:', message)
	}
	
	// 生命周期
	onMounted(() => {
		// 注册消息监听器
		messageManager.addListener(messageListener)
		console.log('全局消息组件已挂载，注册监听器')
	})
	
	onUnmounted(() => {
		// 移除消息监听器
		messageManager.removeListener(messageListener)
	})
</script>

